#include "project.h"
#include <sys/ioctl.h>

#include "../drive/light/light.h"
#include "../drive/switch/switch.h"
#include "../drive/tem_hum/temhum.h"

//初始化网络
int tcp_net_init(const char *IP,const char *PORT){

    int s_fd = socket(AF_INET,SOCK_STREAM,0);
	if(s_fd == -1){
		puts("网络初始化失败");
		return -1;
	}

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(IP);
	server_addr.sin_port = htons(atoi(PORT));

	return s_fd;
}

/*根据用户设置的阈值维护恒定环境线程*/
void *hold_envthread(void *argv)
{
	int which;
	while(1){
		//读取环境数据
		//温湿度
		memset(&data, 0, sizeof(tmp_hum_t));
    	read(tmp_hum_fd, &data, sizeof(tmp_hum_t));

		conthume = 125.0 * data.hum / 65536 - 6;
   		conttemp = 175.72 * data.tmp / 65536 - 46.85;
		//光强
		read(light_fd, &contlux, sizeof(int));

		//printf("当前的温度：%d, 湿度：%d, 光强：%d\n", conttemp, conthume, contlux);

		//与参考值比较，决定是否开启对应的设备
		printf("灯光的下限：%d\n", luxdown);
		which = 1;
		if (contlux < luxdown && luxdown > 0){//灯光低于下限打开灯光
			 ioctl(switch_fd, SWITCH_ON, &which);
		}
		if (contlux > luxup && luxup > 0){
			ioctl(switch_fd, SWITCH_OFF, &which);
		}
		
		which = 1;
		if (conttemp > tempup && tempup > 0){//温度超过上限打开风扇
			 ioctl(switch_fd, SWITCH_ON, &which);
		}
		if (conttemp < tempdown && tempdown > 0){
			ioctl(switch_fd, SWITCH_OFF, &which);
		}
		
		which = 2;
		if (conthume < humedown && conthume > 0){//湿度超过下限打开马达
			ioctl(switch_fd, SWITCH_ON, &which);
		}
		if (conthume > humeup && humeup > 0){
			ioctl(switch_fd, SWITCH_OFF, &which);
		}

		printf("恒定数据设置\n");
		sleep(10);
	}
}

/*获取环境数据线程*/
void do_getenv(msg_t *msg){

	// tempup = msg->limitset.tempup;
	// tempdown = msg->limitset.tempdown;
	// humeup = msg->limitset.humeup;
	// humedown = msg->limitset.humedown;
	// luxup = msg->limitset.luxup;
	// luxdown = msg->limitset.luxdown;

	// printf("环境光下限：%d\n", msg->limitset.luxdown);

	//温湿度
	memset(&data, 0, sizeof(tmp_hum_t));
    read(tmp_hum_fd, &data, sizeof(tmp_hum_t));
    msg->envdata.hume = 125.0 * data.hum / 65536 - 6;
    msg->envdata.temp = 175.72 * data.tmp / 65536 - 46.85;

	//光强
	read(light_fd, &msg->envdata.lux, sizeof(int));

	//读取设备数据，赋值给 buf.envdata成员
	send(s_fd, msg, sizeof(msg_t), 0);
}

/*设置阈值线程*/
void do_setlimit(msg_t *msg){
	
	printf("接收到阈值：%d	%d	%d	%d	%d	%d\n", 
	msg->limitset.tempup, msg->limitset.tempdown, 
	msg->limitset.humeup, msg->limitset.humedown, 
	msg->limitset.luxup, msg->limitset.luxdown);
	//将 buf.limitset字段中的数据赋值给参考变量
	//将参考变量数据写入到文件中，方便设备下次启动时有一个合理的参考值
	//将执行结果返回给服务器
	//线程结束
	tempup = msg->limitset.tempup;
	tempdown = msg->limitset.tempdown;
	humeup = msg->limitset.humeup;
	humedown = msg->limitset.humedown;
	luxup = msg->limitset.luxup;
	luxdown = msg->limitset.luxdown;

	send(s_fd, msg, sizeof(msg_t), 0);
}

/*控制设备线程*/
void do_ctrldev(msg_t *msg){

	//根据 buf 中devctrl 字段对应的位，控制设备的启停操作
	int which;
	//照明
	which = 0;
	msg->switchset.swlight != 0 ? ioctl(switch_fd, SWITCH_ON, &which) : ioctl(switch_fd, SWITCH_OFF, &which);
	
	which = 1;
	msg->switchset.swfan != 0 ? ioctl(switch_fd, SWITCH_ON, &which) : ioctl(switch_fd, SWITCH_OFF, &which);

	which = 2;
	msg->switchset.swmotor != 0 ? ioctl(switch_fd, SWITCH_ON, &which) : ioctl(switch_fd, SWITCH_OFF, &which);

	printf("灯的状态：%d，风扇的状态：%d，马达的状态：%d\n", 
			msg->switchset.swlight, msg->switchset.swfan, msg->switchset.swmotor);

	send(s_fd, msg, sizeof(msg_t), 0);
}
/*打开驱动文件*/
int open_drive_fd(void){
	
	switch_fd = open("/dev/switch",O_RDWR);
	if(switch_fd == -1){
		puts("switch打开驱动文件失败");
		return -1;
	}
	tmp_hum_fd = open("/dev/temhum",O_RDWR);
	if(tmp_hum_fd == -1){
		puts("temhum打开驱动文件失败");
		return -1;
	}
	light_fd = open("/dev/light",O_RDWR);
	if(light_fd == -1){
		puts("light打开驱动文件失败");
		return -1;
	}
	return 0;
}
